package znbkCNtxl.CNTXL_PPT;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.znbk.znbklibrary.base.BaseRecyclerAdapter;
import com.example.znbk.znbklibrary.base.BaseRecyclerViewHolder;
import com.example.znbk.znbklibrary.util.LancooUtils;
import com.example.znbk.znbklibrary.widget.CustomTextViewToPicture;
import com.example.znbk.znbklibrary.widget.ScaleButton;
import com.lancoo.znbkxx.R;

import java.util.List;

import znbkCNtxl.GetNtxlJson.CNtxlPaperEntity;

public class CNtxlPptAnswerAdapter extends BaseRecyclerAdapter {

    private Context context;
    private List<CNtxlPaperEntity> CNtxlPaperEntity;
    private int childIndex = -1;
    private CallBackAdapterPpt callBackAdapterPpt;
    private LayoutInflater inflater;

    public CNtxlPptAnswerAdapter(Context context, List<CNtxlPaperEntity> CNtxlPaperEntity, int childIndex, CallBackAdapterPpt callBackAdapterPpt){
        this.context=context;
        this.CNtxlPaperEntity = CNtxlPaperEntity;
        this.childIndex = childIndex;
        this.callBackAdapterPpt = callBackAdapterPpt;
        inflater=LayoutInflater. from(context);
        getAnswerInfor();
    }

    private int subChildOptionCount = -1;
    private int SortIndex = -1;
    private int ChildListSize  =0;
    private void getAnswerInfor(){
        subChildOptionCount = CNtxlPaperEntity.get(childIndex).getQuesFromTK().getChildList().
                get(0).getSubChildList().get(0).getOptionList().size();
        SortIndex = CNtxlPaperEntity.get(childIndex).getQuesFromTK().getChildList().
                get(0).getSubChildList().get(0).getSubChildItem().get(0).getSortIndex();
        ChildListSize = CNtxlPaperEntity.get(childIndex).getQuesFromTK().getChildList().
                get(0).getSubChildList().get(0).getSubChildItem().size();
    }

    @Override
    public int getItemCount() {
        return subChildOptionCount;
    }

    @Override
    public long getItemId(int position){
        return position;
    }

    private TextView mTvChildQuesAsk;
    private LinearLayout mLlOption;
    private ScaleButton mSBtnOptionIndex;
    private TextView mTvOptionContent;

    @Override
    public void onBindViewHolder(BaseRecyclerViewHolder baseRecyclerViewHolder, final int position) {

        mLlOption = baseRecyclerViewHolder.getView(R.id.ll_option);

        mTvChildQuesAsk = baseRecyclerViewHolder.getView(R.id.tv_childQuesAsk);
        if (position == 0) {
            mTvChildQuesAsk.setVisibility(View.VISIBLE);
//            new CustomTextViewToPicture(context,mTvChildQuesAsk,SortIndex+"、 ");
            new CustomTextViewToPicture(context, mTvChildQuesAsk, "从下面选项中挑选合适的选项");
        } else {
            mTvChildQuesAsk.setVisibility(View.GONE);
        }


        mSBtnOptionIndex = baseRecyclerViewHolder.getView(R.id.sbtn_optionIndex);
        String OptionIndex = CNtxlPaperEntity.get(childIndex).getQuesFromTK().getChildList().
                get(0).getSubChildList().get(0).getOptionList().get(position).getOption();
        mSBtnOptionIndex.setText(OptionIndex);
        mSBtnOptionIndex.setTag(position);
        mSBtnOptionIndex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int SubmitState = CNtxlPaperEntity.get(childIndex).getFavouriteAndNote().getSubmitState();
                if (SubmitState == 2) {
                    return;
                }

                String StuAnswer = LancooUtils.asciiToString(String.valueOf(65 + position));
                Log.e("mTvOptionContent", "===============StuAnswer=====111111==========" + StuAnswer);

                callBackAdapterPpt.callClickWhichIndex(StuAnswer);
                doRefresh(position);
            }
        });

        mTvOptionContent = baseRecyclerViewHolder.getView(R.id.tv_optionContent);
        String OptionContent = CNtxlPaperEntity.get(childIndex).getQuesFromTK().getChildList().
                get(0).getSubChildList().get(0).getOptionList().get(position).getContent();
        mTvOptionContent.setText(OptionContent);
        mTvOptionContent.setTag(position);
        mTvOptionContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int SubmitState = CNtxlPaperEntity.get(childIndex).getFavouriteAndNote().getSubmitState();
                if (SubmitState == 2) {
                    return;
                }

                String StuAnswer = LancooUtils.asciiToString(String.valueOf(65 + position));
                callBackAdapterPpt.callClickWhichIndex(StuAnswer);
                doRefresh(position);
            }
        });

//        if(position==subChildOptionCount-1){
//            int SubmitState = CNtxlPaperEntity.get(childIndex).getFavouriteAndNote().getSubmitState();
//            if(SubmitState==2){
//                mLlOption.removeAllViews();
//            }else {
//                View mView = new View(context);
//                mView.setBackgroundColor(Color.parseColor("#ffffff"));
//                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 300);
//                mLlOption.addView(mView, lp);
//            }
//        }

        int SubmitState = CNtxlPaperEntity.get(childIndex).getFavouriteAndNote().getSubmitState();
        if(SubmitState==2){
            mLlOption.removeAllViews();
        }else {
            if (position == subChildOptionCount - 1) {
                mLlOption.removeAllViews();
                View mView = new View(context);
                mView.setBackgroundColor(Color.parseColor("#ffffff"));
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 300);
                mLlOption.addView(mView, lp);
            }
        }

    }

    @Override
    public View getHolderView(ViewGroup parent, int position) {
        View view = inflater.inflate(R.layout.cntxl_adapter_item_answer_ppt,parent, false);
        return view;
    }

    public void doRefresh(int position){
        //输入学生作答
        int AnsweredCount=0;
        int AnsweredCountTrue=0;
        Log.e("==AXC===","==================小题个数=====wwww=========="+ChildListSize);
        for(int i=0;i<ChildListSize;i++){
            String StuAnswer =  CNtxlPaperEntity.get(childIndex).getFavouriteAndNote().getChildList().
                    get(0).getSubChildList().get(0).getSubChildItem().get(i).getStuAnswer();
            if(!StuAnswer.equals("")){
                AnsweredCount = AnsweredCount+1;
            }

            String ItemAnswer =  CNtxlPaperEntity.get(childIndex).getFavouriteAndNote().getChildList().
                    get(0).getSubChildList().get(0).getSubChildItem().get(i).getItemAnswer();
            if(ItemAnswer.equals(StuAnswer)){
                AnsweredCountTrue = AnsweredCountTrue+1;
            }
            Log.e("==AXC===","==================完成个数=====wwww=========="+AnsweredCount);
        }
        //若作答完成则显示答案，记录作答状态，否则亦然
        if (AnsweredCount == ChildListSize) {
            Log.e("==choosepos===","==================语音刷新=====全部做完==========");
            CNtxlPaperEntity.get(childIndex).getFavouriteAndNote().setSubmitState(2);
            callBackAdapterPpt.callHideRecordMiView();
            callBackAdapterPpt.callRefreshClickAnswer();
        }else{
            CNtxlPaperEntity.get(childIndex).getFavouriteAndNote().setSubmitState(0);
        }

    }

    public interface CallBackAdapterPpt {
        void callHideRecordMiView();
        void callClickWhichIndex(String stuAnswer);
        void callRefreshClickAnswer();
    }


}